using System;
using NUnit.Framework;
using ValidationFramework.Configuration;
using ValidationFramework.Extensions;

namespace ValidationFramework.Tests.Configuration
{
    [TestFixture]
    public class RequiredStringRuleConfigReaderTest
    {

        #region Methods

        [Test]
        public void ReadConfig()
        {
            const string validatorXml = "<rule errorMessage='hello' typeName='RequiredStringRule' initialValue='hello2' ruleSet='foo' />";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
			
            var rule = (RequiredStringRule)ConfigurationService.GetRule(ruleData, typeof(bool).TypeHandle);
            Assert.AreEqual("hello", rule.ErrorMessage);
            Assert.AreEqual("hello2", rule.InitialValue);
            Assert.IsTrue(rule.TrimWhiteSpace);

        }

        [Test]
        public void ReadConfigIgnoreSpaces()
        {
            const string validatorXml = "<rule errorMessage='hello' typeName='RequiredStringRule' ignoreSpaces='true'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (RequiredStringRule)ConfigurationService.GetRule(ruleData, typeof(bool).TypeHandle);
            Assert.AreEqual("hello", rule.ErrorMessage);
            Assert.IsTrue(rule.TrimWhiteSpace);

        }

        [Test]
        public void ReadConfigIgnoreCase()
        {
            const string validatorXml = "<rule errorMessage='hello' typeName='RequiredStringRule' initialValue='aa' ignoreCase='false'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var requiredStringRule = (RequiredStringRule)ConfigurationService.GetRule(ruleData, typeof(bool).TypeHandle);
            Assert.AreEqual("hello", requiredStringRule.ErrorMessage);
            Assert.IsFalse(requiredStringRule.IgnoreCase);

        }
        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ReadConfigIgnoreCaseNoInitialValue()
        {
            const string validatorXml = "<rule errorMessage='hello' typeName='RequiredStringRule' ignoreCase='false'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var requiredStringRule = (RequiredStringRule)ConfigurationService.GetRule(ruleData, typeof(bool).TypeHandle);
            Assert.AreEqual("hello", requiredStringRule.ErrorMessage);
            Assert.IsFalse(requiredStringRule.IgnoreCase);

        }
        [Test]
        public void ReadConfigNoTrimWhiteSpace()
        {
            const string validatorXml = "<rule errorMessage='hello' typeName='RequiredStringRule' trimWhiteSpace='false'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (RequiredStringRule)ConfigurationService.GetRule(ruleData, typeof(bool).TypeHandle);
            Assert.AreEqual("hello", rule.ErrorMessage);
            Assert.IsFalse(rule.TrimWhiteSpace);

        }


        [Test]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "String cannot be empty.\r\nParameter name: initialValue")]
        public void ReadConfigEmptyInitialValueException()
        {
            const string validatorXml = "<rule typeName='RequiredStringRule' initialValue=''/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            ConfigurationService.GetRule(ruleData, typeof(bool).TypeHandle);

        }

        [Test]
        public void ReadConfigSimple()
        {
            const string validatorXml = "<rule typeName='RequiredStringRule'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (RequiredStringRule)ConfigurationService.GetRule(ruleData, typeof(bool).TypeHandle);
            Assert.IsNull(rule.ErrorMessage);
            Assert.IsTrue(rule.TrimWhiteSpace);
        }
        #endregion


    }
}